package com.serein.service.impl;

import com.serein.dao.UserDao;
import com.serein.domain.User;
import com.serein.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @author SeRein
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 登录判断
     *
     * @param user 用户信息
     * @return 信息是否正确，1表示正确，0表示错误
     */
    public int login(User user) {
        return userDao.login(user);
    }

    /**
     * 返回数据库所有用户
     *
     * @return list用户集合
     */
    public List<User> findAll() {
        return userDao.findAll();
    }

    /**
     * 删除指定id的用户
     *
     * @param id 用户id
     * @return 删除结果
     */
    public int delUser(int id) {
        return userDao.delUser(id);
    }

    /**
     * 批量添加数据
     *
     * @return
     */
    public int addUsers() {
        System.out.println("addusers-service执行");
        User user = new User();
        System.out.println("user创建完成");
        int count = 0;
        for (int i = 0; i < 20; i++) {
            String username = "xuLong" + i + "号";
            String userpassword = "1";
            user.setId(String.valueOf(i));
            user.setUsername(username);
            user.setUserpassword(userpassword);
            user.setAge(i);
            System.out.println(user);
            int re = userDao.addUser(user);
            count += re;
        }
        return count;
    }
}